package com.wsseieiei.fireballgame.items;

import com.wsseieiei.fireballgame.Main;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ItemBallPlus extends Item {
//    public ItemBallPlus(Properties p_41383_) {
//        super(p_41383_);
//    }

    public ItemBallPlus() {
        super(new Properties().tab(Main.FIREBALL_GAME_TAB).stacksTo(1));
    }

    protected ItemStack findAmmo(Player player) {
        if (this.isBall2test(player.getItemInHand(InteractionHand.OFF_HAND))) {
            return player.getItemInHand(InteractionHand.OFF_HAND);
        } else if (this.isBall2test(player.getItemInHand(InteractionHand.MAIN_HAND))) {
            return player.getItemInHand(InteractionHand.MAIN_HAND);
        } else {
            for (int i = 0; i < player.getInventory().getContainerSize(); ++i) {
                ItemStack itemstack = player.getInventory().getItem(i);
                if (this.isBall2test(itemstack)) {
                    return itemstack;
                }
            }
            return ItemStack.EMPTY;
        }
    }

    protected boolean isBall2test(ItemStack stack) {
        return stack.getItem() instanceof ItemBallPlus;
    }

    public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level level, Player playerIn, @NotNull InteractionHand handIn) {
        ItemStack itemstack = playerIn.getItemInHand(handIn);
        boolean flag = !this.findAmmo(playerIn).isEmpty();
        InteractionResultHolder<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, level, playerIn, handIn, flag);
        if (ret != null) return ret;
        level.playSound(null, playerIn.getX(), playerIn.getY(), playerIn.getZ(), SoundEvents.UI_BUTTON_CLICK, SoundSource.PLAYERS, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
        if (!playerIn.getAbilities().instabuild && !flag) {
            return InteractionResultHolder.fail(itemstack);
        } else {
            playerIn.startUsingItem(handIn);
            List<Entity> needAttackEntitys = playerIn.level.getEntities(playerIn, new AABB(playerIn.getX() - 5.0D, playerIn.getY(), playerIn.getZ() - 5.0D, playerIn.getX() + 5.0D, playerIn.getY() + 5.0D, playerIn.getZ() + 5.0D), Entity::isAlive);
            for (Entity needAttackEntity : needAttackEntitys) {
//                needAttackEntitys.get(i).hurt(DamageSource.badRespawnPointExplosion(),32767);
                if (!(needAttackEntity.getType()==playerIn.getType())){
                    needAttackEntity.kill();
                }
            }
            if(!playerIn.isCreative()){
                playerIn.getCooldowns().addCooldown(this,2000);
            }
            return InteractionResultHolder.consume(itemstack);
        }
    }


}